package na.learn.java.corepatterns.hashing.division;

import java.util.LinkedList;
import java.util.ListIterator;

public class HashTable {

	private static final int SIZE = 20;

	private LinkedList[] table = new LinkedList[SIZE];

	public boolean add(Object k, Object v) {
		Entry entry = new Entry(k, v);
		if (table[k.hashCode() % SIZE] == null)
			table[k.hashCode() % SIZE] = new LinkedList();

		table[k.hashCode() % SIZE].add(entry);

		return false;
	}

	public void printHashTable() {
		int tableIndex = 0;
		for (int count = 0; count < 20; count++) {
			if (table[count] != null) {
				System.out.print("bucket# " + count + " --- ");
				for (ListIterator iter = table[count].listIterator(); iter.hasNext();)
					System.out.print("**" + iter.next());
				System.out.println("");
			}
		}
	}

	public static void main(String[] args) {
		
		MyType myObj = null;
		HashTable table = new HashTable();
		char ch = 'A';
		
		for(int count = 1; count < 20; count ++){
			ch = (char)(ch + 1);			
			myObj = new MyType("" + ch + ch + ch , count);
			table.add(myObj, myObj);
		}
		
		
		
/*
		MyType myObj1 = new MyType("ABC", 11);
		MyType myObj2 = new MyType("BCD", 22);
		MyType myObj3 = new MyType("CDE", 33);
		MyType myObj4 = new MyType("DEF", 44);
		MyType myObj5 = new MyType("EFG", 55);
		MyType myObj6 = new MyType("XYZ", 6);
		MyType myObj7 = new MyType("IRT", 777);

		MyType myObj1_1 = new MyType("ABC", 12);
		MyType myObj1_2 = new MyType("BCD", 12);

		HashTable table = new HashTable();
		table.add(myObj1, myObj1);
		table.add(myObj1_1, myObj1_1);
		table.add(myObj2, myObj2);
		table.add(myObj3, myObj2);
		table.add(myObj4, myObj2);
		table.add(myObj5, myObj2);
		table.add(myObj1_2, myObj2);
		table.add(myObj6, myObj6);
		table.add(myObj7, myObj6);

*/		table.printHashTable();

	}

	private class Entry {
		private Object key;

		private Object value;

		public Entry(Object k, Object v) {
			this.key = k;
			this.value = v;
		}

		public Object getKey() {
			return key;
		}

		public void setKey(Object key) {
			this.key = key;
		}

		public Object getValue() {
			return value;
		}

		public void setValue(Object value) {
			this.value = value;
		}
		
		@Override
		public String toString() {
			
			return "key: " + key/* + ", value: " +value*/;
		}

	}

}
